<!--
/*********************************************************************************
 * SugarCRM is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/

/*********************************************************************************
 * {APP.LBL_CURRENCY_SYM}Header: /cvsroot/sugarcrm/sugarcrm/modules/Contacts/EditView.html,v 1.22 2004/07/16 04:04:42 sugarclint Exp {APP.LBL_CURRENCY_SYM}
 ********************************************************************************/
-->
<!-- BEGIN: main -->
<!-- BEGIN: error -->
<span class="error">{ERROR}</span>
<!-- END: error -->


			<form enctype="multipart/form-data" name="MailMerge" method="POST" action="index.php">
			<input type="hidden" name="module" value="MailMerge">
			<input type="hidden" name="step" id="step" value="{STEP}">
			<input type="hidden" name="action" value="index">
			<input type="hidden" name="return_module" value="{RETURN_MODULE}">
			<input type="hidden" name="return_id" value="{RETURN_ID}">
			<input type="hidden" name="return_action" value="{RETURN_ACTION}">		
			<input type="hidden" name="selected_objects" id="selected_objects">
<p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="edit view">
<tr>
<td>
	<table border="0" cellspacing="0" cellpadding="0">
	<tr>
	<th align="left" scope="row" colspan="2"><h4 class="">{MAIL_MERGE_HEADER_STEP_2}</h4></th>
	</tr>
	<tr><td><slot>&nbsp;</slot></td></tr>
	<tr>
	<td><input type='text' id="searchText" name="searchText"></td>
	<td><input type="button" id="searchSubmit" value="Search" onClick="search();" class='button'></td>
	</tr>
	<tr><td>{MAIL_MERGE_CAMPAIGN_PROSPECT_SELECTOR}</td></tr>
	</table>
	<table>
	<tr>
		<td>
		{MAILMERGE_PREV}
		<span id="result_count"></span>
		{MAILMERGE_NEXT}
		</td>
		</tr>
	<tr>
	<td  colspan='1'>
	
		<table border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td scope="row">Available
					<table>
						<tr>
							<td><slot><select id="display_objs" name="display_objs[]" size="10" multiple="multiple" ></select></slot></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</td>
	<td>
	{MAILMERGE_LEFT_TO_RIGHT}
	<br>
	{MAILMERGE_RIGHT_TO_LEFT}
	</td>
	<td  colspan='1'>
	
		<table border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td scope="row">Selected
					<table>
						<tr>
							<td><slot><select id="selected_objs" name="selected_objs[]" size="10" multiple="multiple" >{MAILMERGE_PRESELECTED_OBJECTS}</select></slot></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</td>
	</tr>
</table>
</td>
</tr>
<tr><td>{MAIL_MERGE_CONTAINS_CONTACT_INFO}</td></tr>
</table>
</p>
<p><table width="100%" cellpadding="2" cellspacing="0" border="0"><tr> 
	<td align="left"><input id="prevButton" title="{MOD.LBL_BACK}" accessKey="" class="button" type="submit" name="button" value="  {MOD.LBL_BACK}  "  onclick="this.form.step.value='1'; return true;"></td>
	<td align="right"><input id="nextButton" title="{MOD.LBL_NEXT}" accessKey="" class="button" type="submit" name="button" value="  {MOD.LBL_NEXT}  "  onclick="set_selected_items();return validateForm();"></td>
</tr>
</table>  </p>
	</form>
{JAVASCRIPT}
<script type="text/javascript" src="include/JSON.js?s={SUGAR_VERSION}&c={JS_CUSTOM_VERSION}"></script>
<script type="text/javascript" src="include/jsolait/init.js?s={SUGAR_VERSION}&c={JS_CUSTOM_VERSION}"></script>
<script type="text/javascript">{JSON_CONFIG_JAVASCRIPT}</script>
<script type="text/javascript" src="include/javascript/jsclass_base.js?s={SUGAR_VERSION}&c={JS_CUSTOM_VERSION}"></script>
<script type="text/javascript" src="include/javascript/jsclass_async.js?s={SUGAR_VERSION}&c={JS_CUSTOM_VERSION}"></script>

<script>
var module = "{MAILMERGE_MODULE}";
var mOffset = 0;
var max = 20;
var mWhere = "{MAILMERGE_WHERE}";
var orderBy = '';

function ResponseObj()
{
}

ResponseObj.prototype.response = function (result) 
{	 

	var prevButton = document.getElementById("prevButton");
	var nextButton = document.getElementById("nextButton");
	
	var prevItems = document.getElementById("prevItems");
	var nextItems= document.getElementById("nextItems");
	var dispCount = max;
	var dispMin = mOffset;
	if(result.result_count < max)
	{
		dispCount = result.result_count;
		nextItems.disabled = true;
	}
	if(result.result_count - max == mOffset)
	{
		nextItems.disabled = true;
	}
	else
	{
		nextItems.disabled = false;
	}
	if(mOffset == 0)
	{
		prevItems.disabled = true;
	}
	else
	{
		prevItems.disabled = false;
	}
	if(result.result_count > 0)
	{
		dispMin = mOffset + 1;
	}
	var spanText = "("+(dispMin) + " - " +(mOffset+dispCount) + " of " + result.result_count+")";
	document.getElementById("result_count").innerHTML = spanText;
	var displayObjs = document.getElementById("display_objs");
	displayObjs.options.length = 0;
	var isLeadOrContact = false;
	if(module == 'Contacts' || module == 'Leads' || module == 'CampaignProspects')
	{
		isLeadOrContact = true;
	}
	for (i = 0; i < result.entry_list.length; i++)
	{
		var id = '';
		var name = '';
		var name1 = '';
		var name2 = '';
		id = result.entry_list[i]['name_value_list'].id.value;
		if(isLeadOrContact)
		{
			name1 = result.entry_list[i]['name_value_list'].first_name.value;
			name2 = result.entry_list[i]['name_value_list'].last_name.value;
		}
		else
		{
			name1 = '';
			name2 = result.entry_list[i]['name_value_list'].name.value;
		}
		name = name1 + ' ' + name2;
		displayObjs.options[displayObjs.length] = new Option(name, id);
	}
	    
   	prevButton.disabled = false;
	nextButton.disabled = false;
}
function increaseOffset()
{
	mOffset += 20;
}

function decreaseOffset()
{
	mOffset -= 20;
}

function getObjects()
{
	var prevButton = document.getElementById("prevButton");
	var nextButton = document.getElementById("nextButton");
	
	prevButton.disabled = true;
	nextButton.disabled = true;
	getOrderBy();
	
	//rrs
	//mWhere = "campaigns.id = '9be5dd91-d20b-c5fd-1769-4554d8eede05'";

	query = {"module":module, "offset":mOffset, "where":mWhere, "max":max, "order_by":orderBy};

	req_id = global_rpcClient.call_method('get_objects_from_module',query);
	
	var res = new ResponseObj();
    global_request_registry[req_id] = [ res,'response']; 

}

function moveRight()
{
	var displayObjs =  document.getElementById("display_objs");
	var selObjs = document.getElementById("selected_objs");
	
	for (i=0;i<displayObjs.options.length;i++)
	{
		if(displayObjs[i].selected)
		{
			var current = displayObjs.options[i];
			selObjs.options.add(new Option(current.text, current.value));
		}
	}
	//remove them now
	for (i = displayObjs.length - 1; i>=0; i--) {
	try
	{
		if(displayObjs.options[i].selected)
		{
			displayObjs.remove(i);		
		}
	}
		catch(e){}
		
	}
}

function moveLeft()
{
	var selObjs = document.getElementById("selected_objs");
	var displayObjs =  document.getElementById("display_objs");
	for (i=0;i<selObjs.options.length;i++)
	{
		if(selObjs[i].selected)
		{
			var current = selObjs.options[i];
			displayObjs.options.add(new Option(current.text, current.value));
		}
	}
	for (i = selObjs.length - 1; i>=0; i--) {
	try
	{
		if(selObjs.options[i].selected)
		{
			selObjs.remove(i);

		}
	}
		catch(e){}
		
	}
}

function set_selected_items()
{
var selObjs = document.getElementById("selected_objs");

var containsContactInfo = document.getElementById("contains_contact_info");
var step = document.getElementById("step");
var selected_objects = '';

for(i=0; i < selObjs.options.length ;i++)
{
         selected_objects += selObjs.options[i].value+"="+selObjs.options[i].text.replace(/&/g, "##")+"&";
}

	document.MailMerge.selected_objects.value = selected_objects;
//BEGIN CHANGE 38959 -- raagaard
	if(containsContactInfo != null && containsContactInfo.options[containsContactInfo.selectedIndex].value)
//END CHANGE 38959 -- raagaard
	{
		step.value = "3";
	}
	else
	{
		step.value = "4";
	}

}

function search(searchText)
{
	var searchInput = document.getElementById("searchText");
	searchText = searchInput.value;
	var where = '';
	if(searchText != '')
	{
		if(module == 'Contacts' || module == 'Leads')
		{
			where = module+".first_name like '%"+searchText+"%' OR "+module+".last_name like '%"+searchText+"%'";
		}
		else if(module == 'CampaignProspects'){
			var prospectType = document.getElementById("campaign_prospect_type");
			var lmodule = prospectType.value.toLowerCase();
			var campign_where = "{MAILMERGE_WHERE}";
			where = lmodule+".first_name like '%"+searchText+"%' OR "+lmodule+".last_name like '%"+searchText+"%'";
			if(campign_where)
				where += " AND "+campign_where ;
			where += "  AND related_type = #"+lmodule+"#"
		}
		else
		{
			where = module+".name like '"+searchText+"%'";
		}
	}
	mWhere = where.toLowerCase();
	getObjects();
}

function getOrderBy()
{
	var order = '';
	if(module == 'Contacts' || module == 'Leads')
	{
		order = module+".last_name";
	}
	else
	{
		order = module+".name";
	}
	
	orderBy = order.toLowerCase();
}
function validateForm()
{
	var selObjs = document.getElementById("selected_objs");
	
	if(selObjs.options.length > 0)
	{
		return true;
	}
	else
	{
		alert("Please select at least one item.");
		return false;
	}	
}
if({MAILMERGE_GET_OBJECTS}){
	getObjects();
}
</script>
<!-- END: main -->
